﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportService2Ref;
    using WMS6.Web.UserControls;

    public class WorkClassRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15631;
        protected GridView gvWorkClass;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblAllTotalAmount;
        protected Label lblAllTotalAmountText;
        protected Label lblMsg;
        protected Label lblWorkClassList;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal totalAmount;
        protected UpdatePanel up1;

        private void BindData2WorkClassList(WorkClassRptQueryEntity workClassRptQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(workClassRptQueryEntity.CurrentPage);
            DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetWorkClassRptDataSet(workClassRptQueryEntity);
            this.gvWorkClass.Visible = true;
            base.BindData2GridView(this.gvWorkClass, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            WorkClassRptQueryEntity workClassRptQueryEntity = this.GetWorkClassQueryEntity();
            workClassRptQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = workClassRptQueryEntity.CurrentPage;
            workClassRptQueryEntity.CurrentPage = "0";
            try
            {
                using (DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetWorkClassRptDataSet(workClassRptQueryEntity))
                {
                    base.Export2Excel("WorkClassRpt.xls", "15631", this.gvWorkClass, ds);
                }
            }
            finally
            {
                workClassRptQueryEntity.IsGetAll = false;
                workClassRptQueryEntity.CurrentPage = currentPageIndex;
                this.SetWorkClassQueryEntity(workClassRptQueryEntity);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            WorkClassRptQueryEntity workClassRptQueryEntity = this.GetWorkClassQueryEntity();
            workClassRptQueryEntity.CurrentPage = "0";
            this.BindData2WorkClassList(workClassRptQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            WorkClassRptQueryEntity workClassRptQueryEntity = this.GetWorkClassQueryEntity();
            workClassRptQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2WorkClassList(workClassRptQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            WorkClassRptQueryEntity workClassRptQueryEntity = this.GetWorkClassQueryEntity();
            workClassRptQueryEntity.WORK_CLASS_ID = "";
            workClassRptQueryEntity.FORKLIFT_DRIVER = "";
            workClassRptQueryEntity.DOC_TYPE = "";
            workClassRptQueryEntity.WORK_TYPE = "";
            workClassRptQueryEntity.IS_HOLIDAY = "";
            workClassRptQueryEntity.StartDateTime = "";
            workClassRptQueryEntity.EndDateTime = "";
            this.BindData2WorkClassList(workClassRptQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            WorkClassRptQueryEntity workClassRptQueryEntity = this.GetWorkClassQueryEntity();
            if (int.Parse(workClassRptQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                workClassRptQueryEntity.CurrentPage = (int.Parse(workClassRptQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2WorkClassList(workClassRptQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            WorkClassRptQueryEntity workClassRptQueryEntity = this.GetWorkClassQueryEntity();
            if (int.Parse(workClassRptQueryEntity.CurrentPage) > 0)
            {
                workClassRptQueryEntity.CurrentPage = (int.Parse(workClassRptQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2WorkClassList(workClassRptQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            WorkClassRptQueryEntity workClassRptQueryEntity = this.GetWorkClassQueryEntity();
            this.BindData2WorkClassList(workClassRptQueryEntity);
        }

        private WorkClassRptQueryEntity GetWorkClassQueryEntity()
        {
            return (base.PageQueryEntitySession as WorkClassRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                WorkClassRptQueryEntity workClassRptQueryEntity = this.GetWorkClassQueryEntity();
                workClassRptQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2WorkClassList(workClassRptQueryEntity);
            }
        }

        protected void gvWorkClass_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15631", this.gvWorkClass);
        }

        protected void gvWorkClass_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if ((((Label) e.Row.FindControl("lblAmount")) != null) && !string.IsNullOrEmpty(((Label) e.Row.FindControl("lblAmount")).Text))
                {
                    this.totalAmount += decimal.Parse(((Label) e.Row.FindControl("lblAmount")).Text);
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblAllTotalAmountText.Text = double.Parse(this.totalAmount.ToString()).ToString();
            }
        }

        protected void gvWorkClass_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                WorkClassRptQueryEntity workClassRptQueryEntity = this.GetWorkClassQueryEntity();
                workClassRptQueryEntity.SortField = e.SortExpression;
                if (workClassRptQueryEntity.SortDirection == "ASC")
                {
                    workClassRptQueryEntity.SortDirection = "DESC";
                }
                else if (workClassRptQueryEntity.SortDirection == "DESC")
                {
                    workClassRptQueryEntity.SortDirection = "ASC";
                }
                this.BindData2WorkClassList(workClassRptQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            WorkClassRptQueryEntity workClassRptQueryEntity = new WorkClassRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    workClassRptQueryEntity.WORK_CLASS_ID = this.PreviousPage.WorkClassID;
                    workClassRptQueryEntity.FORKLIFT_DRIVER = this.PreviousPage.ForkliftDriver;
                    workClassRptQueryEntity.DOC_TYPE = this.PreviousPage.DocType;
                    workClassRptQueryEntity.WORK_TYPE = this.PreviousPage.WorkType;
                    workClassRptQueryEntity.IS_HOLIDAY = this.PreviousPage.IsHoliday;
                    workClassRptQueryEntity.StartDateTime = this.PreviousPage.StartDate;
                    workClassRptQueryEntity.EndDateTime = this.PreviousPage.EndDate;
                }
                else
                {
                    workClassRptQueryEntity.WORK_CLASS_ID = "";
                    workClassRptQueryEntity.FORKLIFT_DRIVER = "";
                    workClassRptQueryEntity.DOC_TYPE = "";
                    workClassRptQueryEntity.WORK_TYPE = "";
                    workClassRptQueryEntity.IS_HOLIDAY = "";
                    workClassRptQueryEntity.StartDateTime = "";
                    workClassRptQueryEntity.EndDateTime = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            workClassRptQueryEntity.IsGetAll = false;
            workClassRptQueryEntity.CurrentPage = "0";
            workClassRptQueryEntity.PageSize = this.gvWorkClass.PageSize.ToString();
            workClassRptQueryEntity.SortField = " WORK_DRIVER";
            workClassRptQueryEntity.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvWorkClass.PageSize;
            this.SetWorkClassQueryEntity(workClassRptQueryEntity);
            this.BindData2WorkClassList(workClassRptQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvWorkClass);
                this.LoadData();
            }
        }

        private void SetWorkClassQueryEntity(WorkClassRptQueryEntity workClassRptQueryEntity)
        {
            base.PageQueryEntitySession = workClassRptQueryEntity;
        }

        public WorkClassRptQuery PreviousPage
        {
            get
            {
                return (WorkClassRptQuery) base.PreviousPage;
            }
        }
    }
}

